package com.example.mydownloader.mymodule.gson

import com.example.mydownloader.mymodule.log.LogUtil
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException


/**
 * @Description:
 * GsonUtil
 * @author Wallace
 * @Date 2022/1/24 14:29
 */
object GsonUtil {
    private val gson = Gson()

    /**
     * 将对象转成json字符串
     */
    fun toJson(any: Any): String {
        return gson.toJson(any)
    }

    fun <T> toAny(jsonString: String?, classType: Class<T>): T? {
        return try {
            gson.fromJson(jsonString, classType)
        } catch (e: JsonSyntaxException) {
            LogUtil.e("$e")
            null
        }
    }
}